﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using SevenShine;
using DG.Tweening;

public class PlayerControl : MonoBehaviour {

    public static PlayerControl Instance;

    public Transform role;
    public Transform target1;
    public Transform target2;
    public Transform target3;
    public Transform target4;
    public Animator ani;
    public AudioClip Pass;
    public AudioClip RunClip;
    public AudioClip DialogClip;

    int a = 0;
    private AudioSource audios;

    // Use this for initialization
    void Start () {
        Instance = this;
        ani = GetComponent<Animator>();

        audios = GameObject.Find("Main Camera").GetComponent<AudioSource>();
        
    }
	
	void BeginMove()
    {
        audios.clip = RunClip;
        audios.Play();
        audios.loop = true;

        PlayerState.Instance.SetState(PlayerState.State.RUN, target1, 3);
        StartCoroutine("CameraEnable");
    }

    IEnumerator CameraEnable()
    {
        yield return new WaitForSeconds(3);
        audios.Stop();

        CameraControl.Instance.firstTouch = true;
    }

	void Update ()
    { 
        AnimatorStateInfo stateInfo = ani.GetCurrentAnimatorStateInfo(0);
        if (stateInfo.IsName("XiaoQi@idle01") && a==0)
        {
            Debug.Log(a);
            PlayerState.Instance.SetState(PlayerState.State.IDLE,target1,0);  
            a +=1;
        }
	}

    public void WalkToDoor()
    {
        audios.clip = RunClip;
        audios.Play();
        audios.loop = true;

        PlayerState.Instance.SetState(PlayerState.State.RUN, target3, 1);
        StartCoroutine("CameraMove");
    }

    IEnumerator CameraMove()
    {
        yield return new WaitForSeconds(1.5f);
        audios.Stop();

        CameraControl.Instance.LeaveDoor();
    }

    void EnterDoor()
    {
        audios.clip = RunClip;
        audios.Play();
        audios.loop = true;

        PlayerState.Instance.SetState(PlayerState.State.RUN, PlayerControl.Instance.target4, 3);
        StartCoroutine("NextLevel");
    }

    IEnumerator NextLevel()
    {
        //过关音效
        GameObject.Find("Canvas").GetComponent<AudioSource>().clip = Pass;
        GameObject.Find("Canvas").GetComponent<AudioSource>().Play();

        yield return new WaitForSeconds(3);
        audios.Stop();
        SceneManager.LoadScene("004");
    }

    public void Dialog()
    {
        Instantiate(Resources.Load("Prefabs/UI/DialogImage"),GameObject.Find("Canvas").transform);
        Text text = GameObject.Find("DialogImage(Clone)/Text").GetComponent<Text>();
        Image image= GameObject.Find("DialogImage(Clone)").GetComponent<Image>();

        audios.clip = DialogClip;
        audios.Play();
        audios.loop = false;
        float length = DialogClip.length;

        text.text = "";
        text.DOText(LanguageMgr.GetInstance().ShowText("Level3_Dialog"), length).SetEase(Ease.Linear).OnComplete(()=>
        {
            text.DOFade(0,0.1f);
            image.DOFade(0,0.1f);
            EnterDoor();
        });
    }
}
